<?php

class SearchServer
{
    protected $db;

    public function __construct()
    {
//        $dsn = 'mysql:dbname=jingyue_cms;host=da5c0b93eb51460eab3cfe1aa35e28e7in01.internal.cn-east-3.mysql.rds.myhuaweicloud.com:3306';
//        $user = 'xiake';
//        $password = 'Ailing1314';
        $dsn = 'mysql:dbname=jingyue_cms;host=127.0.0.1:3306';
        $user = 'root';
        $password = '123456';
        try {
            $this->db = new PDO($dsn, $user, $password);
        } catch (PDOException $e) {
            echo 'Connection failed: ' . $e->getMessage();
        }
    }

    /**
     * 过滤非法字符串
     * @return void
     */
    protected function filterInput($param)
    {
        $pass = true;
        $forbidArr = [' ', 'select', 'SELECT', 'update', 'UPDATE', 'delete', 'DELETE', 'insert', 'INSERT', 'drop', 'DROP'];
        foreach ($forbidArr as $value) {
            if (strpos($param, $value) !== false) {
                $pass = false;
                break;
            }
        }
        return $pass;
    }

    /**
     * 匹配八位字符串
     * @return false|int
     */
    protected function string8($str)
    {
        return preg_match('/^\w{8}$/', $str);
    }

    /**
     * 获取投放链接信息
     * @param $chanurlno
     * @return array
     */
    public function getLinkRow($chanurlno): array
    {
//        $sql = 'select * from app_channel_link where no = :no  limit 1';
//        $stmt = $this->db->prepare($sql);
//        $stmt->execute(array('no' => $chanurlno));
//        foreach ($stmt as $row) {
//            $searchRes[] = $row;
//        }
        $searchRes = [];
        if (!$this->string8($chanurlno)) {
            return $searchRes;
        }
        $redis = new HredisServer();
        $key = "JING_YUE_CHANNEL_LINK:".$chanurlno;
        $res = $redis->get($key);
        if(!empty($res)){
            $searchRes = json_decode($res,true);
        }else{
            $sql = 'select * from app_channel_link where no = "' . $chanurlno . '" limit 1';
            $res = $this->db->query($sql);
            $searchRes = $res->fetch(PDO::FETCH_ASSOC);
            if(empty($searchRes)){
                $searchRes = [];
            }else{
                $redis->set($key,json_encode($searchRes));
            }
        }
        return $searchRes;
    }

    /**
     * 获取屏蔽规则
     * @param $channo
     * @param $app
     * @param $producer
     * @return array
     */
    public function getRuleRow($channo,$app,$producer){
        $searchRes = [];
        if (!$this->string8($channo) || !$this->filterInput($app) || !$this->filterInput($producer)) {
            return $searchRes;
        }
        $redis = new HredisServer();
        $key = "JING_YUE_FORBID_RULE:".$channo.":".$app.":".$producer;
        $res = $redis->get($key);
        if(!empty($res)){
            $searchRes = json_decode($res,true);
        }else{
            $sql = 'select * from app_ad_shield where channel_no = "' . $channo . '" AND appid = "'.$app.'" AND producer = "'.$producer.'" limit 1';
            $res = $this->db->query($sql);
            $searchRes = $res->fetch(PDO::FETCH_ASSOC);
            if(empty($searchRes)){
                $searchRes = [];
            }else{
                $redis->set($key,json_encode($searchRes));
            }
        }
        return $searchRes;

    }
}